package com.rc.saas.mini.controller.charge;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.utils.RestTemplateUtil;
import com.rc.saas.mini.vo.charge.CommandStartReq;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Feign 调用测试
 * Created by sven on 2019/10/30
 */
@RestController
@RequestMapping("/micro")
public class MicroServiceController {
    private static Logger logger = LogManager.getLogger(MicroServiceController.class);

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    /**
     * http://localhost:8080/micro/test/3
     *
     * @param id
     * @return
     */
    //@ResponseBody
    @GetMapping("/test/{id}")
    public String test(@PathVariable(value = "id") Long id) {
        logger.info("test.id:{}.................", id);

//        logger.error(LogMarkers.SLEUTH_MARKER, "服务链路追踪:{}", id);

        return "microService user=" + id;
    }


    @GetMapping("/start")
    @ResponseBody
    public TResponse start() {

        CommandStartReq startChargeReq = new CommandStartReq();
        startChargeReq.setMemberId(8L);
//            startChargeReq.setCmdCode(SocketConstant.POWER_START);
        startChargeReq.setElecPileId(12583L);
        startChargeReq.setGunNum("1");
//        startChargeReq.setTicketUsingType(commandStartReq.getTicketUsingType());
        //外部订单号
        startChargeReq.setOutOrderNum("123456805");
        startChargeReq.setTenantCode("MA4T4JPE8");  //运营商
//        startChargeReq.setCarNum("粤BFC0746");  //车牌号
        startChargeReq.setAccountType("0");

        //查地址表
        String requestUrl = "http://rc-evcharger2/rc-evcharger/mini/query_start_charge";
        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(startChargeReq);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);
        try {
            logger.info("std入参加密接口,启动充电路由被调用，...............对应url:{},orderSource:{}", requestUrl);
            //通过rest调用远程服务接口
            ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
            logger.info("std入参加密接口,启动充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
            return request.getBody();
        } catch (Exception e) {
            //RPC调用抛出异常时，继续HttpClient调用
            e.printStackTrace();
            return null;
        }
    }

}